package com.xuelang.pipeline.entry.pipeline;

import com.xuelang.pipeline.app.pipeline.PipelineInstanceService;
import com.xuelang.pipeline.app.pipeline.PipelineService;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.IPipelineInstanceService;
import com.xuelang.pipeline.client.pipeline.dto.PipelineInstanceConfigDTO;
import com.xuelang.pipeline.client.pipeline.dto.PipelineInstanceDTO;
import com.xuelang.pipeline.client.pipeline.dto.PipelineInstanceQuery;
import com.xuelang.pipeline.client.pipeline.dto.TriggerPipelineDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 流水线实例控制类
 *
 * @author : yixin.lys
 * @date : 2024-06-26 10:07
 **/
@Slf4j
@RestController
@RequestMapping("/pipelineInstance")
public class PipelineInstanceController implements IPipelineInstanceService {

    @Autowired
    private PipelineInstanceService pipelineInstanceService;
    @Autowired
    private PipelineService pipelineService;

    @Override
    public PageRecord<PipelineInstanceDTO> page(@Valid PageParam<PipelineInstanceQuery> dtoPageParam) {
        return pipelineInstanceService.page(dtoPageParam);
    }

//    @Override
//    public ApiResult<PipelineInstanceConfigDTO> getPipelineInstanceByPipelineCode(String pipelineCode) {
//        return ApiResult.success(pipelineInstanceService.getPipelineInstanceConfig(pipelineCode));
//    }

    @Override
    public ApiResult<PipelineInstanceConfigDTO> getPipelineInstance(String flag, String code) {
        return ApiResult.success(pipelineInstanceService.getInstanceConfig(flag, code));
    }

    @Override
    public ApiResult<Boolean> triggerPipeline(TriggerPipelineDTO triggerPipelineDTO) {
        //设置手动触发
        triggerPipelineDTO.setTriggerType(3);
        return ApiResult.success(pipelineService.triggerPipelineInstance(triggerPipelineDTO));
    }

    @Override
    public ApiResult<Boolean> batchTriggerPipeline(@Valid List<String> pipelineCodeList) {
        return ApiResult.success(pipelineService.batchTriggerPipelineInstance(pipelineCodeList));
    }

    @Override
    public ApiResult<Boolean> cancelPipeline(String pipelineCode) {
        return ApiResult.success(pipelineService.cancelPipelineInstance(pipelineCode));
    }

    @Override
    public ApiResult<Boolean> instanceApprove(String type, String taskUuid) {
        return pipelineInstanceService.instanceApprove(type, taskUuid);
    }
}
